package com.gufe.service.admin.impl.index;

import com.gufe.dto.adminIndex.IndexBarChartDTO;
import com.gufe.dto.adminIndex.IndexLineChartDTO;
import com.gufe.mapper.admin.index.IndexMapper;
import com.gufe.mapper.admin.joinArmy.JoinArmyMapper;
import com.gufe.service.admin.index.IndexService;
import com.gufe.utils.CamelToSnake;
import com.gufe.vo.adminIndex.IndexBarChartVO;
import com.gufe.vo.adminIndex.IndexDateVO;
import com.gufe.vo.adminIndex.IndexPieChartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private IndexMapper indexMapper;
    @Autowired
    private JoinArmyMapper joinArmyMapper;

    //获取方框区数据
    @Override
    public IndexDateVO getIndexData() {
        //预征班申请人数
        Integer preEnrollmentCount = indexMapper.getPreEnrollmentCount();

        //发布通知数
        Integer notificationCount = indexMapper.getNotificationCount();

        // 预储总人数
        Integer totalReserveCount = indexMapper.getTotalReserveCount();

        // 发布轮播图数
        Integer carouselCount = indexMapper.getCarouselCount();

        //入伍人数
        Integer toArmyCount =joinArmyMapper.getTotalCount();

        //意见反馈数 DOTO
        Integer ideaBack = 6;

        return new IndexDateVO(preEnrollmentCount, notificationCount, totalReserveCount, carouselCount,toArmyCount,ideaBack);
    }

    //获取折线图数据
    @Override
    public IndexLineChartDTO getLineChartData(Integer yearRange) {
        //获取当前时间
        LocalDate now = LocalDate.now();
        int currentYear = now.getYear();
        int currentMonth = now.getMonthValue();

        // 构建返回的折线图数据
        List<String> xAxisData = new ArrayList<>();
        List<Integer> preEnrollmentData = new ArrayList<>();
        List<Integer> enrollmentData = new ArrayList<>();

        // 根据 yearRange 来决定查询的数据范围
        if (yearRange == 1) {
            // 查询从1月到当前月的数据
            for (int i = 1; i <= currentMonth; i++) {
                String startDate = currentYear + "-" + String.format("%02d", i) + "-01 00:00:00";//%02d确保该字符串至少有两个字符宽
                // 获取该月的最后一天，避免不同月份天数不同
                String endDate = currentYear + "-" + String.format("%02d", i) + "-" + LocalDate.of(currentYear, i, 1).lengthOfMonth() + " 23:59:59";

                int preEnrollment = indexMapper.preEnrollmentData(startDate, endDate);
                int enrollment = joinArmyMapper.getJoinArmyFromDate(startDate, endDate);

                // 将日期格式化为"年-月"
                xAxisData.add(currentYear + "-" + String.format("%02d", i));
                preEnrollmentData.add(preEnrollment);
                enrollmentData.add(enrollment);
            }
        }else {
            for (int i = currentYear - (yearRange-1); i <= currentYear; i++) {
                String startDate = i + "-01-01 00:00:00";
                String endDate = i + "-12-31 23:59:59";
                int preEnrollment = indexMapper.preEnrollmentData(startDate, endDate);
                int enrollment = joinArmyMapper.getJoinArmyFromDate(startDate, endDate);
                xAxisData.add(i + "年");
                preEnrollmentData.add(preEnrollment);
                enrollmentData.add(enrollment);
            }
        }

        return new IndexLineChartDTO(xAxisData, preEnrollmentData, enrollmentData);
    }

    //获取首页饼图数据
    @Override
    public List<IndexPieChartVO> getIndexPieChartData(String category) {
        //小驼峰转下划线处理
        category = CamelToSnake.convertToSnakeCase(category);
        List<IndexPieChartVO> pieChartData = indexMapper.getPieChartData(category);
        //性别处理
        if("gender".equals(category)) {
            for (IndexPieChartVO item : pieChartData) {
                item.setName("1".equals(item.getName()) ? "男" : "女" );
            }
        }
        return pieChartData;
    }

    //获取首页柱状图数据
    @Override
    public IndexBarChartVO getBarChartData(String category) {
        //如果是age需进行处理

        List<String> categories = new ArrayList<>();
        List<Integer> values = new ArrayList<>();

        List<IndexBarChartDTO> categoriesDateList = new ArrayList<>();
        if("age".equals(category)){
            //得到年龄分布
            categoriesDateList = indexMapper.getBarChartAgeDate(category);
        }else if ("height".equals(category)){
            //得到身高分布
            categoriesDateList = indexMapper.getBarChartHeightDates(category);
        }else{
            //体重分布
            categoriesDateList = indexMapper.getBarChartWeightDates(category);
        }
        for (IndexBarChartDTO item : categoriesDateList) {
            categories.add(item.getCategory());
            values.add(item.getValue());
        }
        IndexBarChartVO indexBarChartVO = new IndexBarChartVO(categories, values);
        return new IndexBarChartVO(categories, values);
    }
}
